package 贪吃蛇;

import com.sun.javafx.image.BytePixelSetter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Time;
import java.util.Random;

/**
 * @ClassName: GamePanel
 * @Auther: Drawin
 * @Date: 2022/7/21 22:35
 * @Description:
 */
//游戏面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
    //蛇的数据结构
    int length;//长度
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String direction;//初始方向→
    //食物定义数据
    int foodX;int foodY;
    Random rd=new Random();
    int score;//积分
    //游戏当前状态：开始 停止
    boolean isStart = false;
    boolean isFaile=false;//游戏状态 失败与否
    //定时器 new出来start就可以用了
    Timer timer = new Timer(100, this);//100ms执行一次

    //构造方法
    public GamePanel() {
        init();
        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);//获得键盘事件
        timer.start();
    }

    //初始化
    public void init() {
        length = 3;
        snakeX[0] = 100;
        snakeY[0] = 100;//头的坐标
        snakeX[1] = 75;
        snakeY[1] = 100;//body1
        snakeX[2] = 50;
        snakeY[2] = 100;//body2
        direction = "R";
        score=0;

        //食物随机分布在界面上
        foodX=25+25*rd.nextInt(34);
        foodY=75+25*rd.nextInt(24);

    }


    //绘制面板,游戏所有组件都用这个画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//clearscreen

        this.setBackground(Color.white);
        Data.head.paintIcon(this, g, 25, 11);
        g.fillRect(25, 75, 850, 600);
        //画积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度"+length,750,30);
        g.drawString("分数"+score,750,55);

        //paint food
        Data.food.paintIcon(this,g,foodX,foodY);//先画食物再画身体，可以让头碰到食物的一瞬间食物刷新，不会身体盖过食物

        //paint snake
        if (direction.equals("R")) {
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (direction.equals("L")) {
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (direction.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (direction.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);//body
        }


        if (isStart==false) {
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("按下空格开始游戏", 300, 300);

        }

        if (isFaile){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("失败,按下空格重新开始", 300, 300);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    //键盘监听
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();//获得键盘按键
        if (keyCode == KeyEvent.VK_SPACE) {//空格开始或暂停游戏
            if (isFaile){
                //restart
                isFaile=false;
                init();
            }else {
                isStart = !isStart;//取反
            }
            repaint();
        }
        if (keyCode == KeyEvent.VK_UP) {
            direction = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            direction = "D";
        } else if (keyCode == KeyEvent.VK_LEFT) {
            direction = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            direction = "R";
        }
    }


    //事件监听-----需要通过固定时间来刷新
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFaile) {
            if(snakeX[0]==foodX&&snakeY[0]==foodY){
                length++;
                score+=10;
                foodX=25+25*rd.nextInt(34);
                foodY=75+25*rd.nextInt(24);
            }
            //右移
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];//后面的往前移动一个单位
                snakeY[i] = snakeY[i - 1];
            }

            if (direction.equals("R")) {
                snakeX[0] = snakeX[0] + 25;
                //边界判断
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
            } else if (direction.equals("L")) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {
                    snakeX[0] = 850;
                }
            } else if (direction.equals("U")) {
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {
                    snakeY[0] = 650;
                }
            } else if (direction.equals("D")) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {
                    snakeY[0] = 75;
                }
            }
            //失败判断，碰到自己就失败
            for (int i=1;i<length;i++){
                if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isFaile=true;
                }
            }
            repaint();
        }
        timer.start();//定时器开始
    }
}
